
import requests

import re
import os

"""
requests模块的编码流程
1.发送URL
2.发起请求
3.获取响应数据
4.持久化存储
"""

# <div class="thumb">
#
# <a href="/article/124638276" target="_blank">
# <img src="//pic.qiushibaike.com/system/pictures/12463/124638276/medium/EWMAC6AZP0YBP4G3.jpg" alt="糗事#124638276" class="illustration" width="100%" height="auto">
# </a>
# </div>
ex = '<div class="thumb">.*?<img src="(.*?)" alt.*?</div>'
header = {
            'User-Agent': 'Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML, like Gecko) Chrome / 86.0.4240.198 Safari / 537.36'
        }


def main():

    for m in range(0, 2):
        print(m)

        url = 'https://www.qiushibaike.com/imgrank/page/' + str(m)
        img_text = requests.get(url=url, headers=header).text
        img_src = re.findall(ex, img_text, re.S)

        # 创建文件夹
        filename = './qiutu' + str(m)
        if not os.path.exists(filename):
            os.mkdir(filename)

        for n in img_src:
            n = 'https:' + n
            img = requests.get(n, headers=header).content
            fp = open(filename + '/' + n[-20:-4] + '.jpg', 'wb')
            fp.write(img)

    print("保存成功")


if __name__ == "__main__":
    main()
